import { createStore } from 'vuex'
import ajax from '@/util/request';

const admin: IAdmin = {
  t_admin_pk: undefined,
  admin_name: undefined,
  token: undefined,
  mobile: undefined,
  funList: [],
  funSet: [],
  pageURLs: [],
}

export default createStore({
  state: {
    admin,
    catOptions: []
  },
  getters: {
    // getters可以返回一个函数
    hasFun(state) {
      return function(funs:string[]) {
        const funSet = state.admin.funSet;
        if (!funSet) {
          return false;
        }
        for (const v of funs) {
          const result = funSet.some(v2 => v2 == v);
          if (result) {
            return true;
          }
        }
        return false;
      }
    }
  },
  mutations: {
    setAdmin(state:any, payload: IAdmin) {
      console.log("重新加载admin:",payload);
      // clearAdminRoute();
      // payload.funList?.forEach(v => {
      //   v.funs.forEach(v2 => {
      //     // 列表
      //     if (v2.type == 2) {
      //       addAdminRoute(<any>v2);
      //     }
      //   })
      // })
      // console.log("payload =",payload);
      state.admin = payload;
    }
  },
  actions: {
    setOptions(context) {
      // 异步加载
      ajax("/nav/queryList",{type: 5}).then(res => {
        if (res.data.status > 0) {
          context.state.catOptions = res.data.result_data;
        }
      }) 
    }
  },
  modules: {
  }
})
